package com.example.demo;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/book")
@RestController
public class BookController {
    private List<Book> bookList = new ArrayList<>();
    @RequestMapping("/login")
    public boolean login(String username, String password){
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return false;
        }
        if(!"admin".equals(username) || !"admin".equals(password)){
            return false;
        }
        return true;
    }
    @RequestMapping("/getBookList")
    public List<Book> getBookList(){
        // 使用一些 mock 数据
        for(int i = 1; i <= 15; i++){
            Book book = new Book();
            book.setId(i);
            book.setName("图书" + i);
            book.setAuthor("作者" + i);
            book.setNum(i * 2);
            book.setPrice(BigDecimal.valueOf(i * 5 + 12));
            book.setPublisher("出版社" + i);
            if(book.getNum() % 5 != 0){
                book.setStatus(1);
                book.setStatusCn("可借阅");
            }else{
                book.setStatus(2);
                book.setStatusCn("不可借阅");
            }
            bookList.add(book);
        }
        return bookList;
    }
}
